package vista;

import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map.Entry;

import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.JOptionPane;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JComboBox;
import javax.swing.JTextArea;

import negocio.NegocioMercado;
import modelo.Articulo;
import modelo.Jugador;
import modelo.Lugar;
import modelo.Mercado;
import modelo.Tablero;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;

import javax.swing.JButton;

import facade.Desplazar;

public class PantallaPrincipal {

	private JFrame frame;
	private JTextField txtIrA;
	private JTextField txtAccion;
	private static Jugador jugador = Jugador.getInstacia();
	private static Tablero tablero = Tablero.getInstacia();
	
	private JComboBox comboBoxIrA;
	private JComboBox comboBoxAccion;
	private JComboBox comboBoxOpcion;
	private JTextField txtEstadisticas;
	private JTextField txtDinero;
	private JTextField txtSemana;
	private JTextField txtProgreso;
	private JTextField textFieldDinero;
	private JTextField textFieldSemana;
	private JTextField textFieldProgreso;
	private JTextField textOpcion;
	private JButton btnComodin;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PantallaPrincipal window = new PantallaPrincipal();
					window.getFrame().setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public PantallaPrincipal() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		setFrame(new JFrame());
		getFrame().setBounds(100, 100, 510, 441);
		getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		comboBoxIrA = new JComboBox();
		
		comboBoxIrA.setBackground(Color.WHITE);
		inicializar_combos();
		
		comboBoxAccion = new JComboBox();
		comboBoxAccion.setBackground(Color.WHITE);
		
		txtIrA = new JTextField();
		txtIrA.setFont(new Font("Dialog", Font.BOLD, 14));
		txtIrA.setEditable(false);
		txtIrA.setBorder(null);
		txtIrA.setBackground(Color.LIGHT_GRAY);
		txtIrA.setText("Ir a");
		txtIrA.setColumns(10);
		
		txtAccion = new JTextField();
		txtAccion.setFont(new Font("Dialog", Font.BOLD, 14));
		txtAccion.setEditable(false);
		txtAccion.setBackground(Color.LIGHT_GRAY);
		txtAccion.setBorder(null);
		txtAccion.setText("Accion");
		txtAccion.setColumns(10);
		
		txtEstadisticas = new JTextField();
		txtEstadisticas.setFont(new Font("Dialog", Font.BOLD, 14));
		txtEstadisticas.setEditable(false);
		txtEstadisticas.setBorder(null);
		txtEstadisticas.setBackground(Color.LIGHT_GRAY);
		txtEstadisticas.setText("Estadisticas");
		txtEstadisticas.setColumns(10);
		
		txtDinero = new JTextField();
		txtDinero.setFont(new Font("Dialog", Font.BOLD, 14));
		txtDinero.setEditable(false);
		txtDinero.setBorder(null);
		txtDinero.setBackground(Color.LIGHT_GRAY);
		txtDinero.setText("Dinero");
		txtDinero.setColumns(10);
		
		txtSemana = new JTextField();
		txtSemana.setFont(new Font("Dialog", Font.BOLD, 14));
		txtSemana.setEditable(false);
		txtSemana.setBorder(null);
		txtSemana.setBackground(Color.LIGHT_GRAY);
		txtSemana.setText("Semana");
		txtSemana.setColumns(10);
		
		txtProgreso = new JTextField();
		txtProgreso.setFont(new Font("Dialog", Font.BOLD, 14));
		txtProgreso.setEditable(false);
		txtProgreso.setBorder(null);
		txtProgreso.setBackground(Color.LIGHT_GRAY);
		txtProgreso.setText("Progreso");
		txtProgreso.setColumns(10);
		
		JTextArea textArea = new JTextArea();
		textArea.setBorder(null);
		textArea.setBackground(Color.LIGHT_GRAY);
		textArea.setEditable(false);
		String estadisticas = new String();
		estadisticas=(jugador.getNombre()+"\n");
		estadisticas = estadisticas+"Educacion: "+jugador.getEducacion()+"\n";
		estadisticas = estadisticas+"Felicidad: "+jugador.getFelicidad()+"\n";
		estadisticas = estadisticas+"Riqueza: "+jugador.getRiqueza()+"\n";
		estadisticas = estadisticas+"Carrera laboral: "+jugador.getCarreraLaboral()+"\n";

		textArea.setText(estadisticas);		
		
		textFieldDinero = new JTextField();
		textFieldDinero.setBorder(null);
		textFieldDinero.setBackground(Color.LIGHT_GRAY);
		textFieldDinero.setEditable(false);
		textFieldDinero.setColumns(10);
		textFieldDinero.setText("$ "+jugador.getRiqueza().toString());
		
		textFieldSemana = new JTextField();
		textFieldSemana.setBackground(Color.LIGHT_GRAY);
		textFieldSemana.setBorder(null);
		textFieldSemana.setEditable(false);
		textFieldSemana.setColumns(10);
		textFieldSemana.setText(tablero.getTiempo().getNroSemana().toString());
		
		textFieldProgreso = new JTextField();
		textFieldProgreso.setBackground(Color.LIGHT_GRAY);
		textFieldProgreso.setBorder(null);
		textFieldProgreso.setEditable(false);
		textFieldProgreso.setColumns(10);
		textFieldProgreso.setText(tablero.getTiempo().getProgreso().toString() + " de 120 hs");
		
				
		JButton btnViajar = new JButton("Viajar");
		btnViajar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Desplazar.desplazar(comboBoxIrA.getSelectedIndex()+1);
				textFieldDinero.setText("$ "+jugador.getRiqueza().toString());
				textFieldSemana.setText(tablero.getTiempo().getNroSemana().toString());
				textFieldProgreso.setText(tablero.getTiempo().getProgreso().toString() + " de 120 hs");
				comboBoxAccion.removeAllItems();
				comboBoxOpcion.setVisible(false);
				textOpcion.setVisible(false);
				btnComodin.setVisible(false);
				Iterator<String> it= tablero.getLugares().get(comboBoxIrA.getSelectedIndex()+1).getListaAcciones().iterator();
				while(it.hasNext()){
					comboBoxAccion.addItem(it.next());
				}				
			}
		});
		
		JButton btnRealizar = new JButton("Realizar");
		btnRealizar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(comboBoxAccion.getSelectedItem().toString().equals("Comprar") ||
				   comboBoxAccion.getSelectedItem().toString().equals("Vender") ||
				   comboBoxAccion.getSelectedItem().toString().equals("Inscribirse") ||
				   comboBoxAccion.getSelectedItem().toString().equals("Cursar")){
					textOpcion.setText(comboBoxAccion.getSelectedItem().toString());
					textOpcion.setVisible(true);
					comboBoxOpcion.setVisible(true);
					llenarComboOpcion();
					btnComodin.setText(comboBoxAccion.getSelectedItem().toString());
					btnComodin.setVisible(true);
				}
				else{
					textOpcion.setVisible(false);
					comboBoxOpcion.setVisible(false);
				}
			}
		});
		
		textOpcion = new JTextField();
		textOpcion.setBorder(null);
		textOpcion.setFont(new Font("Tahoma", Font.BOLD, 14));
		textOpcion.setBackground(Color.LIGHT_GRAY);
		textOpcion.setColumns(10);
		
		comboBoxOpcion = new JComboBox();
		comboBoxOpcion.setBackground(Color.WHITE);
		
		textOpcion.setVisible(false);
		comboBoxOpcion.setVisible(false);
		
		btnComodin = new JButton("Comprar");
		btnComodin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(btnComodin.getText().equals("Comprar")){
					Mercado merc = (Mercado) tablero.getLugares().get(comboBoxIrA.getSelectedIndex()+1);
					NegocioMercado.comprar(merc.getListaElectrodomestico().get(comboBoxOpcion.getSelectedIndex()));
//					MercadoNegocio.comprar(tablero.getLugares().entrySet().);
				}
			}
		});
		btnComodin.setVisible(false);
		
		GroupLayout groupLayout = new GroupLayout(getFrame().getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(21)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(txtEstadisticas, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addContainerGap())
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
							.addGroup(groupLayout.createSequentialGroup()
								.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
									.addGroup(groupLayout.createSequentialGroup()
										.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
											.addComponent(txtProgreso, 0, 0, Short.MAX_VALUE)
											.addComponent(txtSemana, 0, 0, Short.MAX_VALUE)
											.addComponent(txtDinero, GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))
										.addGap(18)
										.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
											.addComponent(textFieldProgreso, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
											.addComponent(textFieldSemana, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
											.addComponent(textFieldDinero, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
									.addGroup(groupLayout.createSequentialGroup()
										.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
											.addComponent(textOpcion, 0, 0, Short.MAX_VALUE)
											.addComponent(txtIrA, GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
											.addComponent(txtAccion, GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE))
										.addPreferredGap(ComponentPlacement.RELATED)
										.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
											.addComponent(comboBoxOpcion, 0, 232, Short.MAX_VALUE)
											.addComponent(comboBoxIrA, Alignment.TRAILING, 0, 232, Short.MAX_VALUE)
											.addComponent(comboBoxAccion, Alignment.TRAILING, 0, 232, Short.MAX_VALUE))))
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
									.addComponent(btnComodin, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(btnRealizar, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(btnViajar, GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE))
								.addGap(35))
							.addGroup(groupLayout.createSequentialGroup()
								.addGap(10)
								.addComponent(textArea, GroupLayout.PREFERRED_SIZE, 349, GroupLayout.PREFERRED_SIZE)
								.addGap(114)))))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(26)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtIrA, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnViajar)
						.addComponent(comboBoxIrA, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnRealizar)
						.addComponent(comboBoxAccion, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtAccion, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(9)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(textOpcion, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(comboBoxOpcion, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnComodin, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
					.addComponent(txtEstadisticas, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(textArea, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtDinero, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(textFieldDinero, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtSemana, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(textFieldSemana, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtProgreso, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(textFieldProgreso, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(31))
		);
		getFrame().getContentPane().setLayout(groupLayout);
	}
	
	public void inicializar_combos(){
		Iterator<Entry<Integer, Lugar>>it = tablero.getLugares().entrySet().iterator();
		while(it.hasNext()){
			comboBoxIrA.addItem(it.next().getValue());
		}
	}

	private void llenarComboOpcion() {
		comboBoxOpcion.removeAllItems();		
		if(comboBoxAccion.getSelectedItem().toString().equals("Comprar")){
			Mercado merc = (Mercado) tablero.getLugares().get(comboBoxIrA.getSelectedIndex()+1);
			Iterator<Articulo> it = merc.getListaElectrodomestico().iterator();
			
			while(it.hasNext())
				comboBoxOpcion.addItem(it.next().toString());
		}
		
		if(comboBoxAccion.getSelectedItem().toString().equals("Vender")){
			
		}

		if(comboBoxAccion.getSelectedItem().toString().equals("Inscribirse")){
			
		}
		
		if(comboBoxAccion.getSelectedItem().toString().equals("Cursar")){
		}
		
		
	}
	
	public JFrame getFrame() {
		return frame;
	}

	public void setFrame(JFrame frame) {
		this.frame = frame;
		frame.getContentPane().setBackground(Color.LIGHT_GRAY);
	}
	public void mostrarMensaje(String mensaje){
		JOptionPane.showMessageDialog(frame, mensaje);
	}
}
